package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.crm.mapper.Gweq023Mapper;
import cn.e4j.modular.crm.service.Gweq023Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class Gweq023ServiceImpl extends ServiceImpl<Gweq023Mapper, GwcsT> implements Gweq023Service {

    @Override
    public List<Dto> getList(Page<Object> page, Dto inDto) {

        ShiroUser shiroUser = ShiroKit.getUser();
        List<Dto> resultDto= Lists.newArrayList();

        String USER_ID = shiroUser.getID();
        String ENT = shiroUser.getDBC();
        String LANG = LangUtils.getLang(shiroUser.getLANG());

        inDto.put("ENT", ENT);
        inDto.put("USER_ID",USER_ID);
        inDto.put("LANG",LANG);

        // 获取可用营运中心列表
        /*List<Dto> sysOrg_= this.baseMapper.getBizCodeList();
        if(sysOrg_.size() == 0){
            return new ArrayList<>();
        }

        List<String> plist = Lists.newArrayList();
        for (Dto org : sysOrg_) {
            plist.add(org.getString("BIZ_CODE"));
        }
        inDto.put("GWCSPLANT",plist);*/

//        List<Dto> gwcss = this.baseMapper.listPage(page,inDto);
        List<Dto> gwcss = InvokeUtil.invokeBeanMethod("gweq023Mapper,listPage",page,inDto);

        for (Dto gwcs : gwcss) {
            inDto.put("GWCSDOCNO",gwcs.getString("GWCSDOCNO"));
            inDto.put("GWCSPLANT",gwcs.getString("GWCSPLANT"));
            inDto.put("GWCSSEQ",gwcs.getString("GWCSSEQ"));
            inDto.put("ENT",ENT);

            //已出货数量
//            Dto gwec = this.baseMapper.getGwec009(inDto);
            Dto gwec = InvokeUtil.invokeBeanMethod("gweq023Mapper,getGwec009",inDto);
            gwcs.put("GWEC009_SUM",gwec.getBigDecimal("GWEC009"));

//            Dto gwef= this.baseMapper.getGwef009_011(inDto);
            Dto gwef= InvokeUtil.invokeBeanMethod("gweq023Mapper,getGwef009_011",inDto);
            //已开票数量
            gwcs.put("GWEF009_SUM",gwef.getBigDecimal("GWEF009"));
            //已开票金额
            gwcs.put("GWEF011_SUM",gwef.getBigDecimal("GWEF011"));
            //已回款金额
//            Dto gweq = this.baseMapper.getGweq013(inDto);
            Dto gweq = InvokeUtil.invokeBeanMethod("gweq023Mapper,getGweq013",inDto);
            gwcs.put("GWEQ013_SUM",gweq.getBigDecimal("GWEQ013"));

            //已出货金额
            BigDecimal ychje=gwcs.getBigDecimal("GWCS004").multiply(gwcs.getBigDecimal("GWEC009_SUM"));
            if(gwcs.getBigDecimal("GWEF011_SUM").compareTo(gwcs.getBigDecimal("GWEQ013_SUM"))>0){
                gwcs.put("WKPYSYE",ychje.subtract(gwcs.getBigDecimal("GWEF011_SUM")));
            }else {
                BigDecimal temp1 = ychje.subtract(gwcs.getBigDecimal("GWEF011_SUM"));
                BigDecimal temp2 = gwcs.getBigDecimal("GWEQ013_SUM").subtract(gwcs.getBigDecimal("GWEF011_SUM"));

                gwcs.put("WKPYSYE",temp1.subtract(temp2));
            }

            gwcs.put("YKPYSYE",gwcs.getBigDecimal("GWEF011_SUM").compareTo(gwcs.getBigDecimal("GWEQ013_SUM")));
            gwcs.put("YSYEHJ",gwcs.getBigDecimal("WKPYSYE").add(gwcs.getBigDecimal("YKPYSYE")));

        }
        return gwcss;
    }

    @Override
    public List<Dto> getGwecPage(Page<Object> page, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT",shiroUser.getDBC());
        dto.put("LANG",LangUtils.getLang(shiroUser.getLANG()));
//        return this.baseMapper.getGwecPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gweq023Mapper,getGwecPage",page,dto);
    }

    @Override
    public List<Dto> getGwedPage(Page<Object> page, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT",shiroUser.getDBC());
        dto.put("LANG",LangUtils.getLang(shiroUser.getLANG()));
//        return this.baseMapper.getGwedPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gweq023Mapper,getGwedPage",page,dto);
    }

    @Override
    public List<Dto> getGwefPage(Page<Object> page, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT",shiroUser.getDBC());
        dto.put("LANG",LangUtils.getLang(shiroUser.getLANG()));
//        return this.baseMapper.getGwefPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gweq023Mapper,getGwefPage",page,dto);
    }

    @Override
    public List<Dto> getGweoPage(Page<Object> page, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT",shiroUser.getDBC());
        dto.put("LANG",LangUtils.getLang(shiroUser.getLANG()));
        return this.baseMapper.getGweoPage(page,dto);
    }

    @Override
    public List<Dto> getGweqPage(Page<Object> page, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        dto.put("ENT",shiroUser.getDBC());
        dto.put("LANG",LangUtils.getLang(shiroUser.getLANG()));
//        return this.baseMapper.getGweqPage(page,dto);
        return InvokeUtil.invokeBeanMethod("gweq023Mapper,getGweqPage",page,dto);
    }

}
